﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace Oiler
{
    public class Date
    {
        private float _day;
        private int _month;
        private int _year;

        public float Day
        {
            get { return _day; }
            set { _day += value; }
        }
        public int Month
        {
            get { return _month; }
            set { _month += value; }
        }
        public int Year
        {
            get { return _year; }
            set { _year += value; }
        }

        public int count = 0;

        public void Initialize(IsolatedStorageSettings saveGame)
        {
            if (!saveGame.Contains("day"))
            {
                Day = 1;
                Month = 1;
                Year = 1860;
            }
        }

        public void Update(bool running)
        {
            count++;

            if (Month == 1 && Day > 31.99 ||
                Month == 3 && Day > 31.99 ||
                Month == 5 && Day > 31.99 ||
                Month == 7 && Day > 31.99 ||
                Month == 8 && Day > 31.99 ||
                Month == 10 && Day > 31.99||
                Month == 12 && Day > 31.99)
            {
                Day = -31;
                Month = 1;
            }
            if (Month == 4 && Day > 30.99 || 
                Month == 6 && Day > 30.99 || 
                Month == 9 && Day > 30.99 || 
                Month == 11 && Day > 30.99)
            {
                Day = -30;
                Month = 1;
            }
            if (Month == 2 && Day > 28.99)
            {
                Day = -28;
                Month = 1;
            }
            if (Month > 12)
            {
                Month = -12;
                Year = 1;
            }

            if (running && count == 5)
            {
                Day = 1;
                count = 0;
            }
        }

        public void Save(IsolatedStorageSettings saveGame)
        {
            if (!saveGame.Contains("day"))
                saveGame.Add("day", Day);

            else
                saveGame["day"] = Day;

            if (!saveGame.Contains("month"))
                saveGame.Add("month", Month);

            else
                saveGame["month"] = Month;

            if (!saveGame.Contains("year"))
                saveGame.Add("year", Year);

            else
                saveGame["year"] = Year;

            saveGame.Save();
        }

        public void Load(IsolatedStorageSettings saveGame)
        {
            if (saveGame.Contains("day"))
                Day = (float)saveGame["day"];

            if (saveGame.Contains("month"))
                Month = (int)saveGame["month"];

            if (saveGame.Contains("year"))
                Year = (int)saveGame["year"];
        }
    }
}
